package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/9/20 9:50 AM
 *
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
 */
public class _121_买卖股票的最佳时机 {

    public int maxProfit(int[] prices) {

        if (prices == null || prices.length <= 1) {

            return 0;
        }

        int maxProfix = 0,min = prices[0];

        for (int i = 1; i < prices.length; i++) {

            if (prices[i - 1] < min) {

                min = prices[i - 1];
            }
            int max = prices[i] - min;

            if (max > 0 && maxProfix < max) {

                maxProfix = max;
            }
        }

        return maxProfix;
    }
}
